set -e
RED="\e[31m"
GREEN="\e[32m"
RES="\e[0m"

dirs=`ls -F | grep -oP '.*(?=/)'`
for dir in $dirs
do
    # 拉取
    while true
    do
        branch=`LC_ALL=C git remote show $dir | awk '/HEAD branch/ {print $NF}'`
        echo "拉取 $dir $branch"
        git subtree pull --prefix=$dir $dir $branch --squash
        if [[ $? == 0 ]]; then
            echo -e "${GREEN}拉取成功${RES}"
            break
        fi
        echo -e "${RED}拉取失败，是否重试？(Y/n):${RES} \c"
        read again
        if [[ $again == "n" || $again == "N" ]]; then
            break
        fi
    done
done

# 推送
while true
do
    git push origin master
    if [[ $? == 0 ]]; then
        echo -e "${GREEN}推送成功${RES}"
        break
    fi
    echo -e "${RED}推送失败，是否重试？(Y/n):${RES} \c"
    read again
    if [[ $again == "n" || $again == "N" ]]; then
        break
    fi
done